#include "stdio.h"
typedef struct{
	int x[200];
	int y[200];
	int length;
}pointlist;
int searchmax(int a[][101],int x,int y,int m,int n,pointlist point);
int main(){
	int m,n,n1,k,i,j,max;
	int a[101][101];
	pointlist point;
	scanf("%d",&n1);
	while(n1--){
		scanf("%d%d%d",&n,&m,&k);
		point.length=0;
		for(j=1;j*j<=k;j++){
			for(i=0;j*j+i*i<=k;i++){
				if(i==0){
					point.x[point.length]=j;
					point.y[point.length++]=i;
				}
				else{
					point.x[point.length]=j;
					point.y[point.length++]=i;
					point.x[point.length]=j;
					point.y[point.length++]=-i;
				}
			}
		}
		for(i=1;i<=n;i++){
			for(j=1;j<=m;j++){
				scanf("%d",&a[i][j]);
			}
		}
		for(j=m-1;j>=1;j--){
			for(i=1;i<=n;i++){
				a[i][j]+=searchmax(a,j,i,m,n,point);
			}
		}

		max=a[1][1];
		for(i=2;i<=n;i++){
			if(a[i][1]>max)
				max=a[i][1];
		}
		printf("%d\n",max);
	}
	return 0;
}

int searchmax(int a[][101],int x,int y,int m,int n,pointlist point){
	int i,j,max;
	max=a[y][x+1];
	for(i=1;i<point.length;i++){
		if(x+point.x[i]>m)
			break;
		else if(y+point.y[i]>n||y+point.y[i]<1)
			continue;
		else{
			if(a[y+point.y[i]][x+point.x[i]]>max)
				max=a[y+point.y[i]][x+point.x[i]];
		}
	}
	return max;
}